/**
 * Ranges.java
 * @author: liuex
 * 2008-1-9 ����11:18:30
 *
 * note: 
 */
package org.lex.utils;

import javax.swing.ListModel;
import javax.swing.tree.TreeNode;

public class Ranges {
	public static Range<String> wrap(final TreeNode root) {
		return new Range<String>() {
			public int endPosition() {
				return root.getChildCount();
			}

			public String getValue(int index) {
				return root.getChildAt(index).toString();
			}

			public int startPosition() {
				return 0;
			}
		};
	}

	public static Range<TreeNode> wrapTreeNode(final TreeNode root) {
		return new Range<TreeNode>() {
			public int endPosition() {
				return root.getChildCount();
			}

			public TreeNode getValue(int index) {
				return root.getChildAt(index);
			}

			public int startPosition() {
				return 0;
			}
		};
	}

	public static Range<Object> wrap(final ListModel model) {
		return new Range<Object>() {
			public int endPosition() {
				return model.getSize();
			}

			public Object getValue(int index) {
				return model.getElementAt(index);
			}

			public int startPosition() {
				return 0;
			}
		};
	}

	public static <T> Range<T> wrap(T[] array) {
		return Ranges.<T> wrap(array, 0, array.length);
	}

	public static <T> Range<T> wrap(final T[] array, final int start, final int end) {
		return new Range<T>() {
			public int startPosition() {
				return start;
			}

			public int endPosition() {
				return end;
			}

			public T getValue(int index) {
				return array[start + index];
			}
		};
	}
}
